﻿/*
 * Created by SharpDevelop.
 * User: 65949
 * Date: 5/12/2011
 * Time: 1:08 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;

namespace Stetson
{
	/// <summary>
	/// Description of MemoryView.
	/// </summary>
	public partial class MemoryView : Form
	{
		public ushort currentAddress = 0;
		public ushort byteWidth = 0;
		public ushort selectedAddress = 0;
		TextBox textBox;
		
		public MemoryView()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			this.ResizeRedraw = true;
			
			this.MouseClick += new MouseEventHandler(MemoryView_MouseClick);
			this.MouseDoubleClick += MemoryView_MouseDoubleClick;
		}

		void MemoryView_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			foreach (KeyValuePair<Rectangle, ushort> pair in Hotspots)
			{
				if (pair.Key.IntersectsWith(new Rectangle(e.X, e.Y, 1, 1)))
				{
					selectedAddress = pair.Value;
					textBox = new TextBox();
					textBox.Location = pair.Key.Location;
					textBox.Size = pair.Key.Size;
					textBox.Text = MainForm.stetson.calculator.ReadMemory(selectedAddress).ToString("x");
					textBox.KeyDown += textBox_KeyDown;
					while (textBox.Text.Length != 2)
						textBox.Text = "0" + textBox.Text;
					Controls.Add(textBox);
				}
			}
			this.Invalidate();
		}

		void textBox_KeyDown (object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
			{
				byte value;
				if (byte.TryParse(textBox.Text, System.Globalization.NumberStyles.HexNumber, null, out value))
                    MainForm.stetson.calculator.ForceWriteMemory(selectedAddress, value);
				Controls.Remove(textBox);
				textBox = null;
			}
		}

		void MemoryView_MouseClick(object sender, MouseEventArgs e)
		{
			if (textBox != null)
			{
				Controls.Remove(textBox);
				textBox = null;
				return;
			}
			foreach (KeyValuePair<Rectangle, ushort> pair in Hotspots)
			{
				if (pair.Key.IntersectsWith(new Rectangle(e.X, e.Y, 1, 1)))
				{
					selectedAddress = pair.Value;
				}
			}
			this.Invalidate();
		}
		
		protected override void OnMouseWheel(MouseEventArgs e)
		{
			if (textBox != null)
			{
				Controls.Remove(textBox);
				textBox = null;
			}
			if (e.Delta != 0)
			{
				currentAddress += (ushort)(e.Delta < 0 ? byteWidth : -byteWidth);
				this.Invalidate();
			}
			base.OnMouseWheel(e);
		}
		
		Dictionary<Rectangle, ushort> Hotspots = new Dictionary<Rectangle, ushort>();
		
		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);
			Hotspots.Clear();
			e.Graphics.Clear(Color.Beige);
			ushort address = currentAddress;
			int x = 3, y = 3;
			Font boldFont = new Font(new FontFamily("Courier New"), 12, FontStyle.Bold);
			Font normalFont = new Font(new FontFamily("Courier New"), 12);
			while (y < this.Height)
			{
				byteWidth = 0;
				string hex = address.ToString("x");
				while (hex.Length != 4)
					hex = "0" + hex;
				e.Graphics.DrawString(hex + ":", boldFont, Brushes.Black, x, y);
				x = (int)(e.Graphics.MeasureString(hex + ":", boldFont).Width + 10);
				while (x < (this.Width - e.Graphics.MeasureString("00", normalFont).Width))
				{
                    hex = MainForm.stetson.calculator.ReadMemory(address).ToString("x");
					while (hex.Length != 2)
						hex = "0" + hex;
					if (address == selectedAddress)
						e.Graphics.FillRectangle(Brushes.LightBlue, x, y,
						                         e.Graphics.MeasureString("00", normalFont).Width,
						                         normalFont.Height);
					Hotspots.Add(new Rectangle(x, y, (int)e.Graphics.MeasureString("00", normalFont).Width,
					                           normalFont.Height), address);
					e.Graphics.DrawString(hex, normalFont, Brushes.Black, x, y);
					x += (int)(e.Graphics.MeasureString(hex, normalFont).Width + 5);
					address++;
					byteWidth++;
				}
				x = 3;
				y += boldFont.Height;
			}
		}
	}
}
